<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body></body>
</html>
<script>
  const url = "http://localhost?a=1&b=2";
  //   截取地址？后面的值，返回一个对象
  const fn = (u) => {
    const str = u.split("?")[1];
    const queryArr = str.split("&");
    const obj = {};
    queryArr.forEach((item) => {
      const arr = item.split("=");
      obj[arr[0]] = arr[1] || "";
    });
    return obj;
  };
  console.log(fn(url));
  //   function tostring(m) {
  //     const arr = Object.keys(m).map((item) => {
  //       if (m[item] || map[item] === 0) {
  //         return item + "=" + m[item];
  //       } else {
  //         return item + "=";
  //       }
  //     });
  //     const str = arr.join("&");
  //     console.log(str);
  //   }
  //   tostring({ a: 1, b: 2 });
</script>
